package com.ashleyleung.jetpack_b_station.taobao

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ashleyleung.jetpack_b_station.base.LoadState
import com.ashleyleung.jetpack_b_station.domain.MapData
import com.ashleyleung.jetpack_b_station.domain.OnSellData
import kotlinx.coroutines.launch
import timber.log.Timber

/**
 * @author lxy
 * @name
 * @class describe 准备数据，数据管理，提供逻辑处理
 * @date 2022-06-28
 * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
 */
class OnSellViewModel : ViewModel() {

    val contentList = MutableLiveData<MutableList<MapData>>()

    val loadState = MutableLiveData<LoadState>()

    private var isLoadMore: Boolean = false

    private val onSellRepository by lazy {
        OnSellRepository()
    }

    companion object {
        //默认第一页
        const val DEFAULT_PAGE = 1
    }

    //当前页
    private var mCurrentPage = DEFAULT_PAGE


    override fun onCleared() {
        super.onCleared()
            //如果有跟本类生命周期不一样的变量可以在此时清理
    }
    /**
     * @name OnSellViewModel
     * @describe 加载首页数据
     * @author liangxy
     * @time 2022/6/28 16:48
     * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
     */
    fun loadContent() {
        isLoadMore = false
        loadState.value = LoadState.LOADING
        this.listContentByPage(mCurrentPage)
    }

    fun refresh() {
        mCurrentPage = 1
        contentList.value?.clear()
        loadContent()
    }

    /**
     * @name OnSellViewModel
     * @describe loadMore
     * @author liangxy
     * @time 2022/6/28 16:48
     * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
     */
    fun loadMore() {
        mCurrentPage++
        isLoadMore = true
        loadState.value = LoadState.LOAD_MORE_LOADING
        this.listContentByPage(mCurrentPage)
    }

    private fun listContentByPage(page: Int) {

        viewModelScope.launch {
            try {
                val onSellList: OnSellData = onSellRepository.getOnSellList(page)
                val oldValue = contentList.value ?: mutableListOf()

                oldValue.addAll(onSellList.tbk_dg_optimus_material_response.result_list.map_data)

                contentList.value = oldValue

                if (onSellList.tbk_dg_optimus_material_response.result_list.map_data.isEmpty()) {

                    loadState.value = if (isLoadMore) LoadState.LOAD_MORE_EMPTY else LoadState.EMPTY
                } else {
                    loadState.value = LoadState.SUCCESS
                }
                Timber.e(
                    "SUCCESS:result: ==%d",
                    onSellList.tbk_dg_optimus_material_response.result_list.map_data.size
                )
            } catch (e: Exception) {
                mCurrentPage--
                e.printStackTrace()
                if (e is NullPointerException) {
                    //没有更多内容的时候，会有一个空指针回永
                    loadState.value = LoadState.LOAD_MORE_EMPTY
                } else {
                    loadState.value = if (isLoadMore) LoadState.LOAD_MORE_EMPTY else LoadState.ERROR
                }
            }

        }

    }


}